# vim:noexpandtab:autoindent:tabstop=8:shiftwidth=8:filetype=make:nocindent:tw=0:
#  This is a template of all of the 'Standard' stuff that we use in all our
#  projects.

CLEANFILES=$(PACKAGE_NAME)-*.tar.gz $(PACKAGE_NAME)-*.tar.xz _buildtemp version
DISTCLEANFILES=*~ dist

EXTRA_DIST =
EXTRA_PROGRAMS=

CLEANFILES += $(EXTRA_PROGRAMS)
CLEANFILES += po/*.gmo src/python/libsmbios_c/__pycache__/ src/pyunit/__pycache__/ src/py-cli/__pycache__/
DISTCLEANFILES += pkg/$(PACKAGE).spec


clean-local:
	-test -z "$(CLEANFILES)" || rm -rf $(CLEANFILES)

distclean-local:
	-test -z "$(DISTCLEANFILES)" || rm -rf $(DISTCLEANFILES)

.PHONY: git-tag
git-tag:
	git tag -s -m "tag for official release: $(PACKAGE_STRING)" v$(PACKAGE_VERSION)

.PHONY: get-version
get-version:
	@echo 'PACKAGE_STRING="$(PACKAGE_STRING)"'
	@echo 'PACKAGE_VERSION="$(PACKAGE_VERSION)"'
	@echo 'PACKAGE="$(PACKAGE)"'

REPLACE_VARS=GETTEXT_PACKAGE PACKAGE_VERSION PACKAGE localedir libdir libexecdir datadir sysconfdir pythondir pkgpythondir pkgdatadir pkgconfdir pkggladedir pkglibexecdir
export $(REPLACE_VARS)

DATA_HOOK_REPLACE=
install-data-hook:
	for i in $(DATA_HOOK_REPLACE); do      \
	    file=$(DESTDIR)/$$i                     ;\
	    for var in $(REPLACE_VARS)	;\
	    do	\
	        var="$$var" perl -p -i -e 's|^\Q$$ENV{var}\E\s*=.*|$$ENV{var}="$$ENV{$$ENV{var}}"|'  $$file;\
	    done	;\
	done

EXEC_HOOK_REPLACE=
install-exec-hook:
	for i in $(EXEC_HOOK_REPLACE); do      \
	    file=$(DESTDIR)/$$i                     ;\
	    for var in $(REPLACE_VARS)	;\
	    do	\
	        var="$$var" perl -p -i -e 's|^\Q$$ENV{var}\E\s*=.*|$$ENV{var}="$$ENV{$$ENV{var}}"|'  $$file;\
	    done	;\
	done

# set default project. can be overridden on cmdline with 'make -e ...'
PROJECT=home:$(USER)

TOPDIR := $(shell cd $(top_builddir);pwd)
BUILDDIR = $(TOPDIR)/_rpmbuild
RPMDIR = $(TOPDIR)
SOURCEDIR = $(TOPDIR)
SPECFILE= $(TOPDIR)/pkg/$(PACKAGE_NAME).spec
SPECDIR = $(TOPDIR)/pkg
SRCRPMDIR = $(TOPDIR)

AM_RPM_DEFINES = --define "_topdir $(TOPDIR)" \
		--define "_builddir $(BUILDDIR)" \
		--define "_rpmdir $(RPMDIR)" \
		--define "_sourcedir $(SOURCEDIR)" \
		--define "_specdir $(SPECDIR)" \
		--define "_srcrpmdir $(SRCRPMDIR)" \
		$(RPM_DEFINES)

.PHONY: rpm srpm
rpm: pkg/$(PACKAGE_NAME).spec dist
	mkdir -p $(BUILDDIR)
	rpmbuild $(AM_RPM_DEFINES) $(CI) -ba --nodeps $(SPECFILE)
	rm -rf $(BUILDDIR)

srpm: pkg/$(PACKAGE_NAME).spec dist
	mkdir -p $(BUILDDIR)
	rpmbuild $(AM_RPM_DEFINES) -bs --nodeps $(SPECFILE)
	rm -rf $(BUILDDIR)

TARBALL=$(PACKAGE_NAME)-*.tar.xz
DESCRIBE=$(shell git describe)
debmagic: dist-xz
	mkdir -p dist/
	cp $(TARBALL) $(DEB_TMP_BUILDDIR)/$(PACKAGE_NAME)_$(PACKAGE_VERSION).orig.tar.xz
	tar -C $(DEB_TMP_BUILDDIR) -xf $(TARBALL)
	cp -ar pkg/debian $(DEB_TMP_BUILDDIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION)/debian
	chmod +x $(DEB_TMP_BUILDDIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION)/debian/rules
	cd $(DEB_TMP_BUILDDIR)/$(PACKAGE_NAME)-$(PACKAGE_VERSION) ; \
	mv debian/changelog.in debian/changelog; \
	dch -b -D $(DIST) "Built from source $(DESCRIBE)" -v "$(PACKAGE_VERSION)-0" ;\
	dpkg-buildpackage -us -uc -D -rfakeroot ; \
	mv ../*.deb $(TOPDIR)/dist/ ; \
	cd -

debs:
	dist=$(DIST); [ "x$${dist}" != "x" ] || dist=`lsb_release -c -s`; \
	tmp_dir=`mktemp -d /tmp/libsmbios.XXXXXXXX` ; \
	make debmagic DEB_TMP_BUILDDIR=$${tmp_dir} DIST=$${dist}; \
	rm -rf $${tmp_dir}
